package TeamService;

import Teamdomain.Architect;
import Teamdomain.Designer;
import Teamdomain.Employee;
import Teamdomain.Programmer;
import com.sun.corba.se.impl.oa.toa.TOA;
import sun.text.resources.cldr.ti.FormatData_ti;

/**
 * @program: Demo2
 * @description:
 * @author: Yan Guang
 * @create: 2020-04-23 12:01
 */
public class TeamService {
    private int counter = 1;
    private final int MAX_MEMBER = 5;
    private Programmer[] team = new Programmer[MAX_MEMBER];
    private int total = 0;

    public Programmer[] getTeam() {
        Programmer[] newteam = new Programmer[total];
        for (int i = 0; i < newteam.length; i++) {
            newteam[i] = team[i];
            return newteam;
        }
        return null;
    }

    public void addMember(Employee e) throws TeamException {
//        成员已满，无法添加
        if (total >= MAX_MEMBER) {
            throw new TeamException("满了");
        }
//        该成员不是开发人员，无法添加
        if (!(e instanceof Programmer)) {
            throw new TeamException("不是开发人员");
        }
//        该员工已在本开发团队中
        Programmer p = (Programmer) e;
        if ("BUSY".equals(((Programmer) e).getStatus().getNAME())) {
            throw new TeamException("已在团队");
        }
//        该员工已是某团队成员
        for (int i = 0; i < team.length; i++) {
            if (team[i].getId() == e.getId()) {
                throw new TeamException("已是团员");
            }
        }
//        该员正在休假，无法添加
        if ("VOCATION".equals(((Programmer) e).getStatus().getNAME())) {
            throw new TeamException("正在休假");
        }
        int a=0,b=0,c=0;
        for (int i = 0; i < team.length; i++) {
            if (team[i] instanceof Programmer) {
                a++;
            } else if (team[i] instanceof Designer) {
                b++;
            } else if (team[i] instanceof Architect) {
                c++;
            }
        }
        if (p instanceof Architect) {
            if (c > 1) {
                throw new TeamException("团队中至多只能有一名架构师，无法添加");
            }
        }
//        团队中至多只能有两名设计师
        if (p instanceof Designer) {
            if (b > 2) {
                throw new TeamException("团队中至多只能有两名设计师，无法添加");
            }
        }
//        团队中至多只能有三名程序员
        if (p instanceof Programmer) {
            if (a > 3) {
                throw new TeamException("团队中至多只能有三名程序员，无法添加");
            }
        }
//        团队中至多只能有一名架构师
//        团队中至多只能有两名设计师
//        团队中至多只能有三名程序员
        p.setStatus(new Status("BUSY"));
        team[total++] = p;
        p.setMemberId(counter++);
    }

    public void removeMember(int memberId) throws TeamException {
        int i=0;
        for (; i < team.length; i++) {
            if (team[i].getMemberId()==memberId){
             team[i].setStatus(new Status("FREE"));
            }
        }
        if (total==i){
            throw new TeamException("不存在");
        }
        for (int j = i; j < total-1; j++) {
            team[i]=team[i+1];
        }
        team[total--]=null;
    }
}
